package com.woniuxy.bookmall.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class AlipayConfig {
    @Autowired
    AliPayProperties aliPayProperties ;

    @Bean
    public AlipayClient alipayClient(){
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayProperties.getGatewayUrl(),
                aliPayProperties.getApp_id(), aliPayProperties.getMerchant_private_key(), aliPayProperties.getFormat(),
                aliPayProperties.getCharset(), aliPayProperties.getAlipay_public_key(), aliPayProperties.getSign_type());
        return alipayClient ;
    }

    @Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) // prototype
    public AlipayTradePagePayRequest alipayTradePagePayRequest(){
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(aliPayProperties.getReturn_url());
        alipayRequest.setNotifyUrl(aliPayProperties.getNotify_url());
        return alipayRequest;
    }
}
